home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / System Extensions / Macintosh Drag and Drop 1.1.1 / Demo Applications / DragText Sources / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-13  |  10.0 KB  |  447 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        menus.c
  4.  *
  5.  *        Menu handling routines and utilities.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Wednesday, February 26, 1992
  10.  *
  11.  *        Copyright © 1992 Apple Computer, Inc.
  12.  *
  13.  */
  14.  
  15. #include <Drag.h>
  16. #include "globals.h"
  17. #include "prototypes.h"
  18. #include "resources.h"
  19.  
  20.  
  21. /*
  22.  *    Given a range of menu items and a menu, FindCheckItem returns the
  23.  *    item number of the first checked item. If no items in the range
  24.  *    are checked, FindCheckItem returns 0 (zero).
  25.  */
  26.  
  27. short FindCheckItem(MenuHandle theMenu, short rangeStart, short rangeEnd)
  28.  
  29. {    short        theMark;
  30.  
  31.     while (rangeStart <= rangeEnd) {
  32.         GetItemMark(theMenu, rangeStart, &theMark);
  33.         if (theMark == checkMark)
  34.             return(rangeStart);
  35.         rangeStart++;
  36.     }
  37.  
  38.     return(0);
  39. }
  40.  
  41.  
  42. /*
  43.  *    Check1Item checks a single menu item in a range of menu items. theItem is
  44.  *    the item number to check. rangeStart and rangeEnd denote a range of menu
  45.  *    items that theItem belongs to. Check1Item checks theItem and removes
  46.  *    any checks from the other items in the given range. Check1Item returns
  47.  *    the item number of the checked item if a newly checked item is selected.
  48.  *    If theItem was already checked or theItem was outside of the given range,
  49.  *    Check1Item returns 0 (zero).
  50.  */
  51.  
  52. short Check1Item(MenuHandle theMenu, short theItem, short rangeStart, short rangeEnd)
  53.  
  54. {    short        theMark;
  55.  
  56.     if ((theItem < rangeStart) || (theItem > rangeEnd))
  57.         return(0);
  58.  
  59.     while (rangeStart <= rangeEnd) {
  60.         if (rangeStart != theItem)
  61.             CheckItem(theMenu, rangeStart, false);
  62.         rangeStart++;
  63.     }
  64.  
  65.     GetItemMark(theMenu, theItem, &theMark);
  66.  
  67.     if (theMark != checkMark) {
  68.         CheckItem(theMenu, theItem, true);
  69.         return(theItem);
  70.     } else {
  71.         return(0);
  72.     }
  73. }
  74.  
  75.  
  76. /*
  77.  *    ItemCheck returns true if the given menu item is checked, false otherwise.
  78.  */
  79.  
  80. short ItemCheck(MenuHandle theMenu, short theItem)
  81.  
  82. {    short        theMark;
  83.  
  84.     GetItemMark(theMenu, theItem, &theMark);
  85.     return(theMark == checkMark);
  86.  
  87. }
  88.  
  89.  
  90. /*
  91.  *    ToggleCheck toggles the check mark on the given menu item. ToggleCheck
  92.  *    returns true if the item becomes checked and false if the item becomes
  93.  *    unchecked.
  94.  */
  95.  
  96. short ToggleCheck(MenuHandle theMenu, short theItem)
  97.  
  98. {    short        theMark;
  99.  
  100.     GetItemMark(theMenu, theItem, &theMark);
  101.     CheckItem(theMenu, theItem, theMark = (theMark != checkMark));
  102.     return(theMark);
  103. }
  104.  
  105.  
  106. /*
  107.  *    SetItemEnable enables or disables a menu item depending on the value
  108.  *    of the enable parameter.
  109.  */
  110.  
  111. void SetItemEnable(MenuHandle theMenu, short theItem, short enable)
  112.  
  113. {
  114.     if (enable) {
  115.         EnableItem(theMenu, theItem);
  116.     } else {
  117.         DisableItem(theMenu, theItem);
  118.     }
  119. }
  120.  
  121.  
  122. /*
  123.  *    Given a string and a menu, ItemStringToItem will return the item number
  124.  *    of the last item that matches the string.
  125.  */
  126.  
  127. short ItemStringToItem(Str255 theItemString, MenuHandle theMenu)
  128.  
  129. {    short        index;
  130.     Str255        theString;
  131.  
  132.     index = CountMItems(theMenu);
  133.     while (index) {
  134.         GetItem(theMenu, index, &theString);
  135.         if (PStrCmp(&theString, theItemString))
  136.             return(index);
  137.         index--;
  138.     }
  139.     return(0);
  140. }
  141.  
  142.  
  143. short FontToItem(short fontFamilyNumber)
  144.  
  145. {    Str255        fontName;
  146.  
  147.     GetFontName(fontFamilyNumber, &fontName);
  148.     return(ItemStringToItem(fontName, GetMHandle(idFontMenu)));
  149. }
  150.  
  151.  
  152. short SizeToItem(short theSize)
  153.  
  154. {    Str255        theString;
  155.  
  156.     if (theSize == 9)
  157.         return(1);
  158.     NumToString((long) theSize, &theString);
  159.     return(ItemStringToItem(theString, GetMHandle(idSizeMenu)));    
  160. }
  161.  
  162.  
  163. short ItemToSize(short theItem)
  164.  
  165. {    long        theSize;
  166.     Str255        theString;
  167.  
  168.     if (theItem == 1)
  169.         return(9);
  170.     GetItem(GetMHandle(idSizeMenu), theItem, &theString);
  171.     StringToNum(&theString, &theSize);
  172.     return((short) theSize);
  173. }
  174.  
  175.  
  176. /*
  177.  *    PrepareMenus is called before the user pulls down a menu from the menu
  178.  *    bar or types a command key equivalent. PrepareMenus enables and disables
  179.  *    menu items within the current program context. Any other menu related
  180.  *    setup may be performed here.
  181.  */
  182.  
  183. void PrepareMenus()
  184.  
  185. {    MenuHandle        theMenu;
  186.     Document        *theDocument;
  187.     short            teSelection, theMode, index;
  188.     TextStyle        theStyle;
  189.     Str255            theStr;
  190.  
  191.     theDocument = IsDocumentWindow(FrontWindow());
  192.     teSelection = (theDocument) && 
  193.                   ((**(theDocument->theTE)).selStart !=
  194.                    (**(theDocument->theTE)).selEnd);
  195.  
  196.     theMenu = GetMHandle(idFileMenu);
  197.  
  198.     SetItemEnable(theMenu, NewItem,   gDocumentCount < MaxDocumentCount);
  199.     SetItemEnable(theMenu, OpenItem,  gDocumentCount < MaxDocumentCount);
  200.  
  201.     SetItemEnable(theMenu, CloseItem, theDocument != 0L);
  202.     SetItemEnable(theMenu, SaveItem, (theDocument) && (theDocument->dirty));
  203.     SetItemEnable(theMenu, SaveAsItem, theDocument != 0L);
  204.     SetItemEnable(theMenu, RevertItem, (theDocument) &&
  205.                                        (theDocument->dirty) &&
  206.                                        (theDocument->fRefNum));
  207.  
  208.     SetItemEnable(theMenu, PageSetupItem, false);
  209.     SetItemEnable(theMenu, PrintItem, false);
  210.  
  211.     theMenu = GetMHandle(idEditMenu);
  212.  
  213.     GetIndString(theStr, MenuStringsID, gCanUndoDrag);
  214.     SetItem(theMenu, iUndo, theStr);
  215.     SetItemEnable(theMenu, iUndo, gCanUndoDrag != slCantUndo);
  216.  
  217.     SetItemEnable(theMenu, iCut, teSelection);
  218.     SetItemEnable(theMenu, iCopy, teSelection);
  219.     SetItemEnable(theMenu, iPaste, theDocument != 0L);
  220.     SetItemEnable(theMenu, iClear, teSelection);
  221.     SetItemEnable(theMenu, iSelectAll, theDocument != 0L);
  222.     SetItemEnable(theMenu, iShowClipboard, false);
  223.  
  224.     theMenu = GetMHandle(idFontMenu);
  225.     SetItemEnable(theMenu, 0, theDocument != 0L);
  226.     if (gFontItem)
  227.         CheckItem(theMenu, gFontItem, false);
  228.     gFontItem = 0;
  229.  
  230.     theMenu = GetMHandle(idSizeMenu);
  231.     SetItemEnable(theMenu, 0, theDocument != 0L);
  232.     if (gSizeItem)
  233.         CheckItem(theMenu, gSizeItem, false);
  234.     gSizeItem = 0;
  235.     index = CountMItems(theMenu);
  236.     while (index) {
  237.         SetItemStyle(theMenu, index, normal);
  238.         index--;
  239.     }
  240.  
  241.     theMenu = GetMHandle(idStyleMenu);
  242.     SetItemEnable(theMenu, 0, theDocument != 0L);
  243.     index = CountMItems(theMenu);
  244.     while (index) {
  245.         CheckItem(theMenu, index--, false);
  246.     }
  247.  
  248.     if (theDocument) {
  249.         theMode = doFont + doFace + doSize;
  250.         TEContinuousStyle(&theMode, &theStyle, theDocument->theTE);
  251.  
  252.         if (theMode & doFont) {
  253.             CheckItem(GetMHandle(idFontMenu),
  254.                       gFontItem = FontToItem(theStyle.tsFont), true);
  255.  
  256.             theMenu = GetMHandle(idSizeMenu);
  257.             index = CountMItems(theMenu);
  258.             while (index) {
  259.                 if (RealFont(theStyle.tsFont, ItemToSize(index)))
  260.                     SetItemStyle(theMenu, index, outline);
  261.                 index--;
  262.             }
  263.         }
  264.  
  265.         theMenu = GetMHandle(idSizeMenu);
  266.         if (theMode & doSize) {
  267.             CheckItem(theMenu, gSizeItem = SizeToItem(theStyle.tsSize), true);
  268.         }
  269.  
  270.         theMenu = GetMHandle(idStyleMenu);
  271.         if (theMode & doFace) {
  272.             if (! theStyle.tsFace)
  273.                 CheckItem(theMenu, iPlainText, true);
  274.             if (theStyle.tsFace & bold)
  275.                 CheckItem(theMenu, iBold, true);
  276.             if (theStyle.tsFace & italic)
  277.                 CheckItem(theMenu, iItalic, true);
  278.             if (theStyle.tsFace & underline)
  279.                 CheckItem(theMenu, iUnderline, true);
  280.             if (theStyle.tsFace & outline)
  281.                 CheckItem(theMenu, iOutline, true);
  282.             if (theStyle.tsFace & shadow)
  283.                 CheckItem(theMenu, iShadow, true);
  284.             if (theStyle.tsFace & extend)
  285.                 CheckItem(theMenu, iExtended, true);
  286.             if (theStyle.tsFace & condense)
  287.                 CheckItem(theMenu, iCondensed, true);
  288.         }
  289.     }
  290. }
  291.  
  292.  
  293. short DoSpecialPaste(Document *theDocument)
  294.  
  295. {    long        size, offset;
  296.     Handle        theData;
  297.  
  298.     size = GetScrap(0L, 'UPRC', &offset);
  299.  
  300.     if (size <= 0)
  301.         return(0);
  302.  
  303.     theData = NewHandle(size);
  304.     GetScrap(theData, 'UPRC', &offset);
  305.  
  306.     HLock(theData);
  307.     PutScrap(size, 'test', *theData);
  308.  
  309.     HUnlock(theData);
  310.     DisposeHandle(theData);
  311. }
  312.  
  313.  
  314.  
  315. /*
  316.  *    DoMenuCommand dispatches menu command routines from a menu select parameter.
  317.  *    This function is called when the user selects a menu item with the mouse
  318.  *    or types a command key equivalent.
  319.  */
  320.  
  321. void DoMenuCommand(long select)
  322.  
  323. {    short        theMenuID, theItem, theFontNumber, result;
  324.     MenuHandle    theMenu;
  325.     Str255        theName;
  326.     WindowPtr    theWindow;
  327.     Document    *theDocument;
  328.     Point        where;
  329.     RGBColor    outColor;
  330.  
  331.     theDocument = IsDocumentWindow(theWindow = FrontWindow());
  332.  
  333.     theItem   = LoWord(select);
  334.     theMenuID = HiWord(select);
  335.     theMenu   = GetMHandle(theMenuID);
  336.     switch(theMenuID) {
  337.         case idAppleMenu:
  338.             switch(theItem) {
  339.                 case AboutItem:
  340.                     result = Alert(256, 0L);
  341.                     break;
  342.                 default:
  343.                     GetItem(GetMHandle(idAppleMenu), theItem, &theName);
  344.                     OpenDeskAcc(theName);
  345.             }
  346.             break;
  347.         case idFileMenu:
  348.             switch(theItem) {
  349.                 case NewItem:
  350.                     DoNewDocument();
  351.                     PrepareMenus();
  352.                     DrawMenuBar();
  353.                     break;
  354.                 case OpenItem:
  355.                     DoOpenDocument();
  356.                     PrepareMenus();
  357.                     DrawMenuBar();
  358.                     break;
  359.                 case CloseItem:
  360.                     if (theDocument) {
  361.                         CloseDocument(theDocument);
  362.                         PrepareMenus();
  363.                         DrawMenuBar();
  364.                     }
  365.                     break;
  366.                 case SaveItem:
  367.                     if (theDocument)
  368.                         DoSaveDocument(theDocument);
  369.                     break;
  370.                 case SaveAsItem:
  371.                     if (theDocument)
  372.                         DoSaveAsDocument(theDocument);
  373.                     break;
  374.                 case RevertItem:
  375.                     DisableUndoDrag();
  376.                     if (theDocument)
  377.                         DoRevertDocument(theDocument);
  378.                     break;
  379.                 case QuitItem:
  380.                     gQuitting = true;
  381.                     while ((gQuitting) && (theDocument = IsDocumentWindow(FrontWindow()))) {
  382.                         CloseDocument(theDocument);
  383.                     }
  384.                     if (gQuitting)
  385.                         gQuit = true;
  386.                     break;
  387.             }
  388.             break;
  389.         case idEditMenu:
  390.             switch(theItem) {
  391.                 case iUndo:
  392.                     DoUndoDrag();
  393.                     break;
  394.                 case iCut:
  395.                     if (theDocument) {
  396.                         DisableUndoDrag();
  397.                         TEICut(theDocument->theTE);
  398.                     }
  399.                     break;
  400.                 case iCopy:
  401.                     if (theDocument) {
  402.                         TECopy(theDocument->theTE);
  403.                     }
  404.                     break;
  405.                 case iPaste:
  406.                     if (theDocument) {
  407.                         if (!DoSpecialPaste(theDocument)) {
  408.                             DisableUndoDrag();
  409.                             TEIPaste(theDocument->theTE, 0L, 0L);
  410.                         }
  411.                     }
  412.                     break;
  413.                 case iClear:
  414.                     if (theDocument) {
  415.                         DisableUndoDrag();
  416.                         TEDelete(theDocument->theTE);
  417.                     }
  418.                     break;
  419.                 case iSelectAll:
  420.                     if (theDocument)
  421.                         DoSelectAllDocument(theDocument);
  422.                     break;
  423.             }
  424.             break;
  425.         case idFontMenu:
  426.             DisableUndoDrag();
  427.             GetItem(GetMHandle(idFontMenu), theItem, &theName);
  428.             GetFNum(&theName, &theFontNumber);
  429.             DoFontSelection(theFontNumber);
  430.             break;
  431.         case idSizeMenu:
  432.             DisableUndoDrag();
  433.             DoSizeSelection(ItemToSize(theItem));
  434.             break;
  435.         case idStyleMenu:
  436.             DisableUndoDrag();
  437.             DoStyleSelection(theItem);
  438.             break;
  439.     }
  440.  
  441.     if (theDocument = IsDocumentWindow(FrontWindow()))
  442.         TEGetHiliteRgn(theDocument->hiliteRgn, theDocument->theTE);
  443.  
  444.     HiliteMenu(0);
  445. }
  446.  
  447.